package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/13 18:49
 * @描述 暂无
 */
public class Solution199 {
    static List<Integer> res = new ArrayList<>();

    public static void main(String[] args) {
        TreeNode n4=new TreeNode(4);
        TreeNode n5=new TreeNode(5);
        TreeNode n3 =new TreeNode(3,null,n4);
        TreeNode n2 =new TreeNode(2,null,n5);
        TreeNode n1 =new TreeNode(1,n2,n3);
        System.out.println(rightSideView(n1));

    }

    public static List<Integer> rightSideView(TreeNode root) {
        dfs(root, 1);
        return res;
    }

    public static void dfs(TreeNode root, int level) {
        if (root == null) return;
        if (res.size() == level - 1) {
            res.add(root.val);
        }

        dfs(root.right, level + 1);
        dfs(root.left, level + 1);
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
